package com.daocao.hua.api.requestInfo;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.daocao.hua.R;
import com.daocao.hua.activity.Application;
import com.daocao.hua.activity.AutonymActivity;
import com.daocao.hua.activity.AutonymSuccessActivity;
import com.daocao.hua.activity.EmailIdentityActivity;
import com.daocao.hua.activity.V3EmailIdentityActivity;
import com.daocao.hua.api.Api;
import com.daocao.hua.api.bean.IndentityType;
import com.daocao.hua.api.bean.KeyValueBean;
import com.daocao.hua.api.bean.LiMuKeyBean;
import com.daocao.hua.api.bean.LiMuSignBean;
import com.daocao.hua.api.bean.LiMuTokenBean;
import com.daocao.hua.api.callback.OperatorContract;
import com.daocao.hua.constant.Const;
import com.daocao.hua.util.Network;
import com.daocao.hua.util.PreferenceUtils;
import com.daocao.hua.util.RxLifeUtil;
import com.daocao.hua.util.ToastUtil;

import java.util.ArrayList;

import kangcheng.com.lmzx_android_sdk_v10.bean.Fuction;
import kangcheng.com.lmzx_android_sdk_v10.commom.sdk.LmzxSDK;
import kangcheng.com.lmzx_android_sdk_v10.commom.sdk.LmzxSdkImpl;
import kangcheng.com.lmzx_android_sdk_v10.util.SharedpreferenceUtils;

/**
 * Created by Administrator on 2017/5/14.
 */

public class OperatorRequest extends BaseRequest<OperatorContract.View> implements OperatorContract.Presenter<OperatorContract.View> {

    public static OperatorRequest create() {
        return new OperatorRequest();
    }

    public final String TAOBAO_TYPE = "taobao";
    public final String JD_TYPE = "jd";
    public final String GONGJIFEN_TYPE = "housefund";
    public final String SHEBAO_TYPE = "socialsecurity";
    public final String YUNYINGSHANG_TYPE = "mobile";
    public final String LINGYING_TYPE = "linkedin";

    @Override
    public void getKey(String deal_id) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        LiMuKeyBean bean = new LiMuKeyBean();
        bean.deal_id = deal_id;
        Api.api_service.getLiMuKey(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getKeySuccess(result);
                });
    }

    @Override
    public void getSign(String params) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        LiMuSignBean bean = new LiMuSignBean();
        bean.params = params;
        Api.api_service.getSign(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getSignSuccess(result);
                });
    }

    @Override
    public void uploadToken(String deal_id, String token, String biztype) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        LiMuTokenBean bean = new LiMuTokenBean();
        bean.deal_id = deal_id;
        bean.token = token;
        bean.biztype = biztype;
        Api.api_service.uploadToken(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.uploadTokenSuccess(result);
                });
    }

    @Override
    public void getMyCertification() {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        Api.api_service.getMyCertification().compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getMyCertificationSuccess(result);
                });
    }


    @Override
    public void getBankRealname() {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        Api.api_service.getBankRealname().compose(RxLifeUtil.NocheckOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (result.getStatus() ==1) {
                        mView.getBankRealnameSuccess(result);
                    } else {
                        mView.getBankRealnameFail(result.getMsg());
                    }
                });
    }


    @Override
    public void getEmailPass() {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        IndentityType bean = new IndentityType();
        bean.style = Const.AutonymTypes.email;
        Api.api_service.getEmailType(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getEmailPassSuccess(result.data);
                });
    }


    public void getAllTypePass(String type) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        IndentityType bean = new IndentityType();
        bean.style = type;
        Api.api_service.getEmailType(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getTaobaoPassSuccess(result.data);
                });
    }


    @Override
    public void getTaobaoPass() {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        IndentityType bean = new IndentityType();
        bean.style = Const.AutonymTypes.taobao;
        Api.api_service.getTaobaoType(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getTaobaoPassSuccess(result.data);
                });
    }

    @Override
    public void getXuexinPass() {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        IndentityType bean = new IndentityType();
        bean.style = Const.AutonymTypes.education;
        Api.api_service.getXueXinType(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getXuexinPassSuccess(result.data);
                });
    }

    @Override
    public void getYunYingPass() {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        IndentityType bean = new IndentityType();
        bean.style = Const.AutonymTypes.mobile;
        Api.api_service.getYunyingType(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getYunYingPassSuccess(result.data);
                });
    }


    //    初始化立木
    public void initLiMu(String uid, String apikey) {
        Application.getInstance().LmsdkInit(apikey, uid);
        LmzxSDK sdk = LmzxSdkImpl.getInstance();
        sdk.registEvent(BaseContext);
        sdk.setTextStyle(BaseContext, 0xFF4a4a4a);
        sdk.setButtonStyle(BaseContext, 0xFF4C81FF);
        sdk.setPageBack(BaseContext, 0xFFf4f4f4);
//        sdk.setBannerColor(BaseContext, 0xFF4C81FF);
        sdk.setBannerTextColor(BaseContext, 0xFFFFFFFF);

        SharedpreferenceUtils.saveBannerColor(BaseContext, 0xff4C81FF);
//        sdk.setBannerColor(BaseContext,0xff4C81FF);
//        LmzxSDK sdk = LmzxSdkImpl.getInstance();
//        //参数依次为apiKey,UID,结果通知地址。这三个都必须
////        http://192.168.1.1/callbackUrl.jsp
////        sdk.init(BaseContext,apiKey,uid,callBakUrl);
//
//        sdk.init(BaseContext, apikey, uid, Const.SERVER_API_URL + "/limu/callback");//apikey 必须的
//        //设置测试的环境,不设置的话默认生产环境。
//        if (Const.IS_DEBUG) {
//            sdk.setEnv(BaseContext, "https://t.limuzhengxin.cn:3443/api/gateway");
////            sdk.setEnv(BaseContext, "https://t.limuzhengxin.cn/api/gateway");
//        } else {
//            sdk.setEnv(BaseContext, "https://api.limuzhengxin.com/api/gateway");
//        }
////        sdk.setEnv(BaseContext, "https://api.limuzhengxin.com/api/gateway");
//        //事件注册
//        sdk.registEvent(BaseContext);
//        sdk.setTextStyle(BaseContext, 0xFF4a4a4a);
//        sdk.setButtonStyle(BaseContext, 0xFFe3483d);
//        sdk.setPageBack(BaseContext, 0xFFf4f4f4);
//        sdk.setBannerColor(BaseContext, 0xFFe3483d);
//        sdk.setBannerTextColor(BaseContext, 0xFFFFFFFF);
    }


    //设置类型
    public void setType(String type) {
        if (type.equals(TAOBAO_TYPE)) {
            LmzxSdkImpl.getInstance().start(BaseContext, Fuction.LMZXSDKFunctionTaoBao, 0 + "", Const.LIMU_CALLBACK);
        } else if (type.equals(YUNYINGSHANG_TYPE)) {
            if (TextUtils.isEmpty(PreferenceUtils.getString(getContext(), "mobile", "")) || Const.IS_DEBUG) {
                LmzxSdkImpl.getInstance().initOperatorPreNo(BaseContext, "", true);
            } else {
                LmzxSdkImpl.getInstance().initOperatorPreNo(BaseContext, PreferenceUtils.getString(getContext(), "mobile", ""), false);
            }

            LmzxSdkImpl.getInstance().start(BaseContext, Fuction.LMZXSDKFunctionMobileCarrie, 0 + "", Const.LIMU_CALLBACK);
        } else if (type.equals(JD_TYPE)) {
            LmzxSdkImpl.getInstance().start(BaseContext, Fuction.LMZXSDKFunctionJD, 0 + "", Const.LIMU_CALLBACK);
        } else if (type.equals(GONGJIFEN_TYPE)) {
            LmzxSdkImpl.getInstance().start(BaseContext, Fuction.LMZXSDKFunctionHousingFund, 0 + "", Const.LIMU_CALLBACK);
        } else if (type.equals(LINGYING_TYPE)) {
            LmzxSdkImpl.getInstance().start(BaseContext, Fuction.LMZXSDKFunctionLinkedIn, 0 + "", Const.LIMU_CALLBACK);
        } else if (type.equals(SHEBAO_TYPE)) {
            LmzxSdkImpl.getInstance().start(BaseContext, Fuction.LMZXSDKFunctionSocialSecurity, 0 + "", Const.LIMU_CALLBACK);
        }
    }


    /**
     * 实名认证界面
     *
     * @param name
     * @param idcard
     */
    public void startAutonym(String name, String idcard) {
        Intent intent = new Intent(getContext(), AutonymActivity.class);
        intent.putExtra("real_name", name);
        intent.putExtra("idcard", idcard);
        BaseContext.startActivity(intent);
        ((Activity) BaseContext).overridePendingTransition(R.anim.right_enter, R.anim.left_out);
    }

    public void gotoEmail(String isIdentity, ArrayList<KeyValueBean> bean) {
        Intent intent = new Intent(getContext(), V3EmailIdentityActivity.class);
        intent.putExtra("isIdentity", isIdentity);

        intent.putExtra("email", bean.get(0).value);
        BaseContext.startActivity(intent);
        ((Activity) BaseContext).overridePendingTransition(R.anim.right_enter, R.anim.left_out);
    }

    public void gotoTaobao(ArrayList<KeyValueBean> bean) {
        Intent intent = new Intent(getContext(), AutonymSuccessActivity.class);
        intent.putParcelableArrayListExtra("info", bean);
        intent.putExtra("type", Const.AutonymTypes.taobao);
//        intent.putExtra("username", username);
//        intent.putExtra("nickName", nickName);
        BaseContext.startActivity(intent);
        ((Activity) BaseContext).overridePendingTransition(R.anim.right_enter, R.anim.left_out);
    }

    public void gotoXueXin(ArrayList<KeyValueBean> bean) {
        Intent intent = new Intent(getContext(), AutonymSuccessActivity.class);
        intent.putExtra("type", Const.AutonymTypes.education);
        intent.putParcelableArrayListExtra("info", bean);
        BaseContext.startActivity(intent);
        ((Activity) BaseContext).overridePendingTransition(R.anim.right_enter, R.anim.left_out);
    }

    public void gotoYunYing(ArrayList<KeyValueBean> bean) {
        Intent intent = new Intent(getContext(), AutonymSuccessActivity.class);
        intent.putExtra("type", Const.AutonymTypes.mobile);
        intent.putParcelableArrayListExtra("info", bean);
        BaseContext.startActivity(intent);
        ((Activity) BaseContext).overridePendingTransition(R.anim.right_enter, R.anim.left_out);
    }
}
